{% extends "admin/base_site.html" %}
{% load i18n static %}

{% block extrastyle %}
    {{ block.super }}
    <link rel="stylesheet" type="text/css" href="{% static 'admin/css/forms.css' %}">
{% endblock %}

{% block content %}
    <div id="content-main">
        <div class="module">
            <h2>导入试题</h2>

            {% if messages %}
                <ul class="messagelist">
                    {% for message in messages %}
                        <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
                    {% endfor %}
                </ul>
            {% endif %}

            {% if step == 1 %}
                <form method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                    <input type="hidden" name="step" value="1">
                    <div class="form-row">
                        <div>
                            <label for="file">选择文件：</label>
                            <input type="file" name="file" id="file" required>
                            <p class="help">支持的文件格式：Excel (.xlsx, .xls) 或 Word (.docx)</p>
                        </div>
                    </div>
                    <div class="submit-row">
                        <input type="submit" value="上传并预览" class="default">
                    </div>
                </form>
            {% elif step == 2 %}
                <form method="post">
                    {% csrf_token %}
                    <input type="hidden" name="step" value="2">
                    <input type="hidden" name="file_path" value="{{ file_path }}">

                    <div class="form-row">
                        <div>
                            <label for="subject">选择科目：</label>
                            <select name="subject" id="subject" required>
                                <option value="">---------</option>
                                {% for subject in subjects %}
                                    <option value="{{ subject.id }}">{{ subject.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>

                    <div class="preview-data">
                        <h3>数据预览</h3>
                        {% if preview_data %}
                            <table>
                                <thead>
                                    <tr>
                                        <th>题目内容</th>
                                        <th>题目类型</th>
                                        <th>选项</th>
                                        <th>答案</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for item in preview_data %}
                                        <tr>
                                            <td>{{ item.content }}</td>
                                            <td>{{ item.question_type }}</td>
                                            <td>{{ item.options }}</td>
                                            <td>{{ item.answer }}</td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        {% else %}
                            <p>无预览数据</p>
                        {% endif %}
                    </div>

                    <div class="submit-row">
                        <input type="submit" value="确认导入" class="default">
                    </div>
                </form>
            {% elif step == 3 %}
                <div class="results">
                    {% if success %}
                        <h3>导入成功</h3>
                        <p>成功导入 {{ imported_count }} 道试题</p>
                    {% else %}
                        <h3>导入失败</h3>
                        <ul class="errorlist">
                            {% for error in errors %}
                                <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                    <div class="submit-row">
                        <a href="{% url 'admin:exam_system_question_changelist' %}" class="button">返回题库列表</a>
                    </div>
                </div>
            {% endif %}
        </div>
    </div>
{% endblock %}